﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace SmallERP.Bll.Com
{
    public class UnityMethod
    {
        public static DateTime GetSystemTime()
        {            
            return DateTime.Now;//先这样，以后再改成从数据库取数据
        }

        /// <summary>
        ///  得到剩余可用的面积数
        /// </summary>
        /// <returns></returns>
        public static double GetUseableArea(string addr, int port)
        {
            double Result=0;
            string strResult = string.Empty;
            byte[] myReadBuffer = new byte[1024];

            string RedStrResult = string.Empty;
            TcpClient client = new TcpClient();
            NetworkStream net = null;

            try
            {
                client.Connect(addr, port);
                client.SendTimeout = 60 * 1000;
                client.ReceiveTimeout = 60 * 1000;
                net = client.GetStream();
                net.ReadTimeout = 60 * 1000;

                byte[] myWriteBuffer = Encoding.Unicode.GetBytes("GA");

                net.Write(myWriteBuffer, 0, myWriteBuffer.Length);

                int tryCount = 0;
                while (tryCount<=3)
                {
                    int redLenth=0;
                    try
                    {
                        redLenth = net.Read(myReadBuffer, 0, myReadBuffer.Length);
                    }
                    catch (Exception)
                    {
                        //return -2;//读取失败
                        Result = -2;
                        net.Close();
                        break;
                    }                   
                    if (redLenth > 0)
                    {
                        strResult = Encoding.Unicode.GetString(myReadBuffer, 0, redLenth);
                        break;
                    }

                    tryCount++;
                }
                net.Close();

                return Convert.ToDouble(strResult);
            }
            catch (Exception)
            {
                return 0;
            }
            finally
            {
                client.Close();
                if (net != null)
                {
                    net.Close();
                    net.Dispose();
                }

            }
        }

        public static string GetDogCode(string addr, int port)//得到加密狗的CODE
        {   
            string strResult = string.Empty;
            byte[] myReadBuffer = new byte[1024];

            string RedStrResult = string.Empty;
            TcpClient client = new TcpClient();
            NetworkStream net = null;

            try
            {
                client.Connect(addr, port);
                client.SendTimeout = 60 * 1000;
                client.ReceiveTimeout = 60 * 1000;
                net = client.GetStream();
                net.ReadTimeout = 60 * 1000;

                byte[] myWriteBuffer = Encoding.Unicode.GetBytes("GN");

                net.Write(myWriteBuffer, 0, myWriteBuffer.Length);

                int tryCount = 0;
                while (tryCount <= 3)
                {
                    int redLenth = 0;
                    try
                    {
                        redLenth = net.Read(myReadBuffer, 0, myReadBuffer.Length);
                    }
                    catch (Exception)
                    {
                        //return -2;//读取失败
                        strResult = string.Empty;
                        net.Close();
                        break;
                    }
                    if (redLenth > 0)
                    {
                        strResult = Encoding.Unicode.GetString(myReadBuffer, 0, redLenth);
                        break;
                    }

                    tryCount++;
                }
                net.Close();

                return strResult;
            }
            catch (Exception)
            {
                return string.Empty;
            }
            finally
            {
                client.Close();
                if (net != null)
                {
                    net.Close();
                    net.Dispose();
                }

            }
        }
    }
}
